package com.example.demo.story.impl;

import com.example.demo.story.NetStoryServiceAbstract;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Objects;

/**
 * @Author TCJ
 * @create 2023/12/26
 */
public class Ling13 extends NetStoryServiceAbstract {
	private String nextUrl = getReadUrl();
	@Override
	public String getBaseUrl() {
		return "https://www.013wx.com";
	}

	@Override
	public String getReadUrl() {
		return getBaseUrl() + "/book/122653065447.html";
	}

	@Override
	public void analysisChapter() {
		Document html = super.getHtml(nextUrl);
		dto.setTitle("changshneg--");
		Elements byClass = html.body().getElementsByClass("section-box");
		Element element = byClass.get(1);
		Elements a = element.getElementsByTag("a");
		for (Element e : a) {
			super.dto.getChapter().put("第" + e.text(),getBaseUrl() + e.attr("href"));
		}
		Element last = html.body().getElementsByClass("index-container").get(0);
		Elements hasNext = last.getElementsByClass("index-container-btn");
		for (Element next : hasNext) {
			String text = next.text();
			if ("下一页".equals(text)){
				nextUrl = getBaseUrl() + next.attr("href");
				analysisChapter();
			}
		}
	}

	@Override
	public String getContent(String url) {
		StringBuilder sb = new StringBuilder();
		nextContent(url,sb);
		return sb.toString();
	}
	private void nextContent(String url,StringBuilder sb) {
		Document html = super.getHtml(url);
		sb.append(Objects.requireNonNull(html.body().getElementById("content")).text());
		Element nextUrl = html.body().getElementById("next_url");
		assert nextUrl != null;
		if ("下一页".equals(nextUrl.text())){
			nextContent(getBaseUrl() + nextUrl.attr("href"),sb);
		}

	}

}
